//主页下拉出现动画
$(".body-div ul>li:nth-child(2)").on("click", function () {
    $(this).children("div").slideToggle(200);
});

$(function () {
    //隔行变色
    $(".shopping-list li:even").css("backgroundColor", "#DBDBDB");

    //实现购物车点击部分商品后在结算清单中显示
    function part_update(object) {
        let part_goods_totalAmount = 0;
        let part_goods_totalPrice = 0;
        let inputList = $(object).children("li").children(".goods").children(".check").children("input");

        for (let i = 0; i < inputList.length; i++) {
            if ($(inputList[i]).hasClass("activeCheck")) {
                part_goods_totalAmount += Number($(inputList[i]).parent().parent().siblings(".amount").children("span").html());
                part_goods_totalPrice += Number($(inputList[i]).parent(".check").parent(".goods").siblings(".subtotal").children("span").html().replace("￥", ""));
            }
        }
        $(".settlement .total-amount span:nth-child(2)").html(part_goods_totalAmount);
        $(".settlement .total-price span:last-child").html("￥" + Number(part_goods_totalPrice).toFixed(2));
    }

    //实现购物车列表点击部分商品后增加激活状态
    $(".shopping-list .parcel-div ul").on("click", "li .goods .check input", function () {
        if ($(this).hasClass("activeCheck")) {
            $(this).removeClass("activeCheck");
            part_update($(this).parent().parent().parent().parent());
        } else {
            $(this).addClass("activeCheck");
            part_update($(this).parent().parent().parent().parent());
        }
    });

    //实时监听购物车商品总数量,大于0件商品button按钮则高亮显示
    let listenerAmount = 0;
    $(".settlement .total-amount span:nth-child(2)").bind("DOMNodeInserted", function (e) {
        listenerAmount = $(e.target).html();
        if (listenerAmount !== "0") {
            $(".settlement .settlement-btn button").css("backgroundColor", "#A10000");
        } else {
            listenerAmount = 0;
            $(".settlement .settlement-btn button").css("backgroundColor", "#b3b3b3");
        }
    });

    //实现结算按钮点击结算时,如果未选择商品则提示用户请选择商品
    $(".settlement .settlement-btn button").on("click", function () {
        if (listenerAmount === 0) {
            window.setTimeout(function () {
                $(".settlement-tip").css("display", "block");
                window.setTimeout(function () {
                    $(".settlement-tip").css("display", "none");
                }, 2500);
            }, 500);
        }
    });
})

//购物车全选功能
let flag = 1;
$("#allChoose").on("click", function () {
    $(".checkItem").prop("checked", $("#allChoose").prop("checked"));
    let amountTotal = 0;
    let priceTotal = 0;
    let amountList = $(".amount span");
    let priceList = $(".subtotal span");
    for (let i = 0; i < amountList.length; i++) {
        amountTotal += Number($(amountList[i]).html());
    }
    for (let j = 0; j < priceList.length; j++) {
        priceTotal += Number($(priceList[j]).html().replace("￥", ""));
    }

    let inputList = $(".goods .check input");

    if (flag === 1) {
        flag = 0;
        $(".settlement .total-amount span:nth-child(2)").html(amountTotal);
        $(".settlement .total-price span:last-child").html("￥" + Number(priceTotal).toFixed(2));

        for (let i = 0; i < inputList.length; i++) {
            $(inputList[i]).addClass("activeCheck");
        }
    } else {
        flag = 1;
        $(".settlement .total-amount span:nth-child(2)").html(0);
        $(".settlement .total-price span:last-child").html("￥0.00");

        for (let i = 0; i < inputList.length; i++) {
            $(inputList[i]).removeClass("activeCheck");
        }
    }
})